// pages/luntanmain/luntanmain.js
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    list: [],
    height: 800,
    showModal:false,
    dialogTitle:'',
    dialogContent:''
  },
  lifetimes: {
    attached: function () {
      //hgz/demand/queryListWithPage
      var tabHeight = ((44 + app.globalData.systemInfo.statusBarHeight) * (750 / app.globalData.systemInfo.windowWidth)); //将高度乘以换算后的该设备的rpx与px的比例
      var windowHeight = app.globalData.windowHeight - 120 - tabHeight;
        this.setData({
          height: windowHeight
       })
       this.onShow();
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onShow(){
      var params = {
        pageNum: 1,
        pageSize: 1000
      }
      app.request.postHeadForm('hgz/post/queryListWithPage', params)
        .then(res => {
          var dataList = res.data.list;
          this.setData({
            list: dataList
          })
        })
    },
    detail(e){
      var id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: '/pages/luntandetail/luntandetail?id=' + id,
      })
    },
    onConfirm() {
      var params ={
        title: this.data.dialogTitle,
        content: this.data.dialogContent
      }
      if(this.data.dialogTitle == ''){
        wx.showToast({
          title: '标题不能为空',
          icon: 'none',
          duration: 1000
        })
        return;
      }
      if (this.data.dialogContent == '') {
        wx.showToast({
          title: '内容不能为空',
          icon: 'none',
          duration: 1000
        })
        return;
      }
      wx.showLoading({
        title: '加载中',
      })
      app.request.postHeadJson('hgz/post/save', params).then(res =>{
        wx.hideLoading();
        if(res.status){
          wx.showToast({
            title: '发帖成功',
            icon: 'none',
            duration: 1000
          })
          this.onShow();
          this.setData({
            showModal: false,
            dialogTitle:'',
            dialogContent:''
          })
        }else{
          wx.showToast({
            title: ''+res.message,
            icon: 'none',
            duration: 1000
          })
        }
      })
  
    },
    submit(){
      this.setData({
        showModal: true
      })
    },
    inputChange1(e){
      this.setData({
        dialogTitle: e.detail.value
      })
    },
    inputChange2(e) {
      this.setData({
        dialogContent: e.detail.value
      })
    },
    onCancel(){
      this.setData({
        showModal: false
      })
    },
    preventTouchMove: function () {
  
    }
  }
})
